import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 四
 */
public class RedisDemoSet {

    public static void test1(Jedis jedis) {
        System.out.println("sadd 和 smember 的使用");
        jedis.flushAll();

        jedis.sadd("key", "111", "222", "333");
        Set<String> result = jedis.smembers("key");
        System.out.println("result: " + result);
    }

    public static void test2(Jedis jedis) {
        System.out.println("sismember 的使用");
        jedis.flushAll();

        jedis.sadd("key", "111", "222", "333");

        boolean result = jedis.sismember("key", "111");
        System.out.println("result: " + result);

        result = jedis.sismember("key", "555");
        System.out.println("result: " + result);
    }

    public static void test3(Jedis jedis) {
        System.out.println("scard 的使用");
        jedis.flushAll();

        jedis.sadd("key", "111", "222", "333");
        long result = jedis.scard("key");
        System.out.println("result: " + result);
    }

    public static void test4(Jedis jedis) {
        System.out.println("spop 的使用");
        jedis.flushAll();

        jedis.sadd("key", "111", "222", "333", "444", "555");
        String result = jedis.spop("key");
        System.out.println("result: " + result);
    }

    public static void test5(Jedis jedis) {
        System.out.println("sinter 的使用");
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        jedis.sadd("key2", "111", "333", "888");
        Set<String> result = jedis.sinter("key1", "key2");
        System.out.println("result: " + result);
    }

    public static void test6(Jedis jedis) {
        System.out.println("sintestore 的使用");
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        jedis.sadd("key2", "111", "333", "888");

        long len = jedis.sinterstore("key", "key1", "key2");
        System.out.println("len: " + len);
        Set<String> result = jedis.smembers("key");
        System.out.println("result: " + result);
    }

    public static void test7(Jedis jedis) {
        System.out.println("sunion 的使用");
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        jedis.sadd("key2", "111", "333", "888");

        Set<String> result = jedis.sunion("key1", "key2");
        System.out.println("result: " + result);
    }

    public static void test8(Jedis jedis) {
        System.out.println("sdiff 的使用");
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        jedis.sadd("key2", "111", "666", "888");

        Set<String> result = jedis.sdiff("key1", "key2");
        System.out.println("result: " + result);

        Set<String> result2 = jedis.sdiff("key2", "key1");
        System.out.println("result2: " + result2);
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
            test1(jedis);
            System.out.println();
            test2(jedis);
            System.out.println();
            test3(jedis);
            System.out.println();
            test4(jedis);
            System.out.println();
            test5(jedis);
            System.out.println();
            test6(jedis);
            System.out.println();
            test7(jedis);
            System.out.println();
            test8(jedis);

        }
    }
}
